import random, json, pyperclip, os, pyautogui, win32gui, win32con
from threading import Event
from time import sleep, time
from libs import get_image_pos, focus_wow_handle
from auto_craft.parts.auto_craft_actions import AutoCraftActions
from auto_craft.parts.auto_salvage_actions import AutoSalvageActions
from auto_craft.parts.auto_order_actions import AutoOrderActions
from auto_craft.parts.auto_set_keyboard_actions import AutoSetKeyboardActions

class RoleController:
  def __init__(self, role_name: str, action: str):
    self.done = False
    self.action = action
    self.role_name = role_name
    self.done_callback = None

  def start(self, done_callback):
    self.done_callback = done_callback
    focus_wow_handle()
    sleep(1)
    self.enter_role()
    # 制造
    if self.action == "craft":
      self.craft_action_group()
    # 每周分解6个卡牌
    if self.action == "salvage":
      self.salvage_action_group()
    if self.action == "order":
      self.order_action_group()
    if self.action == "set_keyboard":
      self.set_keyboard_action_group()
    self.exit_role()

  def set_keyboard_action_group(self):
    AutoSetKeyboardActions().start_actions()
  
  def order_action_group(self):
    AutoOrderActions().start_actions()
  
  def salvage_action_group(self):
    self.get_emails()
    AutoSalvageActions().start_actions()
    pass
  def craft_action_group(self):
    self.get_emails()
    AutoCraftActions().start_actions()
    
  
  def exit_role(self):
    sleep(3)
    pyautogui.press("num0")
    sleep(5)
    search_bar_pos = self.get_search_role_pos()
    if search_bar_pos is None:
      self.run_callback()
      return
    print("role {} logout!!!".format(self.role_name))
    self.run_callback()

  def get_emails(self):
    sleep(2)
    # 使用凯蒂玩具
    pyautogui.press("num4")
    sleep(2)
    # target 凯蒂
    pyautogui.press("num4")
    sleep(2)
    # 与凯蒂交互
    pyautogui.press("9")
    sleep(2)
    # 打开邮箱界面
    pyautogui.press("num4")
    sleep(5)
    pyautogui.press("num3")
    sleep(5)

  
    

  def debug(self):
    login_pos = get_image_pos("images_2/word_login.png", confidence=0.8)
    if login_pos:
      print("login_pos: x={} y={}".format(login_pos.x, login_pos.y))
    qs_pos = get_image_pos("images_2/word_qs.png", confidence=0.8)
    if qs_pos:
      print("qs_pos: x={} y={}".format(qs_pos.x, qs_pos.y))
    dh_pos = get_image_pos("images_2/word_dh.png", confidence=0.8)
    if dh_pos:
      print("dh_pos: x={} y={}".format(dh_pos.x, dh_pos.y))
    mail_pos = get_image_pos("images_2/word_mail.png", confidence=0.8)
    if mail_pos:
      print("mail_pos: x={} y={}".format(mail_pos.x, mail_pos.y))
    craft_pos = get_image_pos("images_2/word_craft.png", confidence=0.8)
    if login_pos:
      print("craft_pos: x={} y={}".format(craft_pos.x, craft_pos.y))
  
  def get_search_role_pos(self):
    count = 1
    pos = None
    while (pos is None) and (count < 10):
      sleep(1)
      pos = get_image_pos("images_2/search_bar_image.png", confidence=0.6)
      count += 1
    return pos
  

  
  
  def get_role_ready_pos(self):
    count = 1
    pos = None
    while (pos is None) and (count < 60):
      sleep(1)
      pos = get_image_pos("images_2/word_login.png")
      count += 1
    return pos

  def enter_role(self):
    search_bar_pos = self.get_search_role_pos()
    if search_bar_pos is None:
      self.run_callback()
      return
      
    print("search_bar_pos: x={} y={}".format(search_bar_pos.x, search_bar_pos.y))
    pyautogui.click(search_bar_pos)
    sleep(2)
    pyperclip.copy(self.role_name)
    sleep(1)
    pyautogui.hotkey('ctrl','v')
    pyautogui.press("enter")
    sleep(2)
    pyautogui.click(x=search_bar_pos.x, y=search_bar_pos.y + 60)
    sleep(3)
    pyautogui.press("enter")
    role_ready_pos = self.get_role_ready_pos()
    if role_ready_pos is None:
      self.run_callback()
      return
    print("role_ready_pos: x={} y={}".format(role_ready_pos.x, role_ready_pos.y))
    print("role {} ready!!!".format(self.role_name))
    sleep(5)
    
  def run_callback(self):
    self.done = True
    if self.done_callback:
      self.done_callback()